package com.siwei.mes.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.Experiment;
import com.siwei.mes.entity.ExperimentCheckItemorder;
import com.siwei.mes.entity.ExperimentItemorder;
import com.siwei.mes.entity.ExperimentTask;
import com.siwei.mes.entity.tgl.TglItemorder;
import com.siwei.mes.entity.tgl.TglTrwd;
import com.siwei.mes.entity.tgl.TglTrwdphbycl;
import com.siwei.mes.enums.CheckEnum;
import com.siwei.mes.mapper.*;
import com.siwei.mes.service.ExperimentService;
import com.siwei.mes.service.TglTrwdphbyclService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * TGL-任务单配合比用量(TglTrwdphbycl)表服务实现类
 *
 * @author linzi
 * @since 2023-12-09 12:01:19
 */
@Service
public class TglTrwdphbyclServiceImpl implements TglTrwdphbyclService {
    private static final Logger log = LoggerFactory.getLogger("tgl");
    @Resource
    private TglTrwdphbyclMapper tglTrwdphbyclMapper;
    @Resource
    private ExperimentCheckItemorderMapper experimentCheckItemorderMapper;
    @Resource
    private ExperimentService experimentService;
    @Resource
    private ExperimentMapper experimentMapper;
    @Resource
    private TglTrwdMapper tglTrwdMapper;
    @Resource
    private ExperimentTaskMapper experimentTaskMapper;
    @Resource
    private ExperimentItemorderMapper experimentItemorderMapper;

    @Override
    public Long create(TglTrwdphbycl entity) {
        tglTrwdphbyclMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(TglTrwdphbycl entity) {
        TglTrwdphbycl tglTrwdphbycl = tglTrwdphbyclMapper.selectByPrimaryKey(entity.getId());
        assertTrue(tglTrwdphbycl == null, "TGL-任务单配合比用量信息不存在");
        tglTrwdphbyclMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        TglTrwdphbycl tglTrwdphbycl = tglTrwdphbyclMapper.selectByPrimaryKey(id);
        assertTrue(tglTrwdphbycl == null, "TGL-任务单配合比用量信息不存在");
        tglTrwdphbyclMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<TglTrwdphbycl> getPage(PageQuery<TglTrwdphbycl, TglTrwdphbycl> query) {
        TglTrwdphbycl searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> tglTrwdphbyclMapper.getList(searchParam));
    }

    @Override
    public TglTrwdphbycl loadById(Long id) {
        return tglTrwdphbyclMapper.loadById(id);
    }

    @Override
    public List<TglTrwdphbycl> getAll(TglTrwdphbycl entity) {
        return tglTrwdphbyclMapper.getList(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void operateTglTrwdphbycl(TglTrwdphbycl entity) {
        log.info("TGL新增或修改任务单配合比用量,入参：{}", JSON.toJSONString(entity));
        /**根据Id判断是否存在*/
        TglTrwdphbycl localInfo = tglTrwdphbyclMapper.selectByFid(entity.getFid());
        if (localInfo != null) {
            if (ObjectUtil.isEmpty(localInfo.getSynctime()) || entity.getSynctime() > localInfo.getSynctime()) {
                entity.setId(localInfo.getId());
                tglTrwdphbyclMapper.updateByPrimaryKeySelective(entity);
            }
        } else {
            tglTrwdphbyclMapper.insertSelective(entity);
        }

        /**任务单号*/
        Integer frwdh = entity.getFrwdh();
        TglTrwd tglTrwd = tglTrwdMapper.selectByFRwdh(frwdh);
        if (tglTrwd == null) {
            return;
        }
        log.info("修改了任务单对应的配合比信息，需要清空之前的阈值记录，任务单id：【{}】",tglTrwd.getId());

        /**只要调用了此接口就需要把这个任务之前有关联的小票信息 直接清空掉*/
        List<ExperimentCheckItemorder> checkItemorderList = experimentCheckItemorderMapper.selectByTaskId(tglTrwd.getId());
        for (ExperimentCheckItemorder checkItemorder : checkItemorderList) {
            experimentCheckItemorderMapper.deleteByPrimaryKey(checkItemorder.getId());
        }

//        /**这里需要生成一个跟换配合比的使用台账记录*/
//        /**新增混凝土试验台账*/
//        Experiment experiment = experimentService.getExperimentInfoByTglTrwdInfo(tglTrwd, CheckEnum.QUICK.getCode());
//        experimentMapper.insertSelective(experiment);
//
//        Long experimentId = experiment.getId();
//
//        /**只要调用了此接口就需要把这个任务之前有关联的小票信息 直接清空掉*/
//        List<ExperimentCheckItemorder> checkItemorderList = experimentCheckItemorderMapper.selectByTaskId(tglTrwd.getId());
//        for (ExperimentCheckItemorder checkItemorder : checkItemorderList) {
//            experimentCheckItemorderMapper.deleteByPrimaryKey(checkItemorder.getId());
//            /**新增试验台账对应小票数据*/
//            experimentItemorderMapper.insertSelective(new ExperimentItemorder().setExperimentId(experimentId).setItemorderId(checkItemorder.getItemorderId()));
//        }
//        /**新增试验台账对应tgl任务单记录*/
//        experimentTaskMapper.insertSelective(new ExperimentTask().setExperimentId(experimentId).setTaskId(tglTrwd.getId()));
//
//        /**修改这个试验台账的代表数量 == 所有跟这个试验台账相关的小票发货数量之和*/
//        List<TglItemorder> tglItemorderList = experimentItemorderMapper.selectByExperimentId(experimentId);
//        double behalfNumber = tglItemorderList.stream().map(TglItemorder::getFhquantity).reduce((float) 0, Float::sum);
//        experiment.setId(experimentId);
//        experiment.setBehalfNumber(behalfNumber);
//        experiment.tglInit(false);
//        experimentMapper.updateByPrimaryKeySelective(experiment);
    }
}
